package com.swc.util;

/**
 * Des和Rsa混合加解密, Rsa对内容的字节数有限制
 * @author huyupeng
 * @date 2020/9/18 8:33
 */
public class DesRsaUtil {

    /**
     * 加密：先试用des加密，再使用rsa公钥加密
     * @param content
     * @param rsaPublicKey
     * @param desSalt
     * @return
     */
    public static String encrypt(String content, String rsaPublicKey, String desSalt) throws Exception {
        String desEncrypt = DesUtil.encrypt(content, desSalt);
        return RsaUtil.encrypt(desEncrypt, rsaPublicKey);
    }

    /**
     * 解密：先试用rsa私钥解密，再使用des解密
     * @param content
     * @param rsaPrivateKey
     * @param desSalt
     * @return
     */
    public static String decrypt(String content, String rsaPrivateKey, String desSalt) throws Exception {
        String rsaDecrypt = RsaUtil.decrypt(content, rsaPrivateKey);
        return DesUtil.decrypt(rsaDecrypt, desSalt);
    }

    public static void main(String[] args) throws Exception {
//        final Map<String, Object> keyMap = RsaUtil.genKeyPair();
//        String publicKey = (String) keyMap.get("publicKey");
//        String privateKey = (String) keyMap.get("privateKey");
        String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCq5cu2nSyGeBnKOn7px3RKNXwgffqBuT1vg5qe7tgpY0T8SZx3SSyfOGuYQ2U5TRAx5HVRiRdghayF1UzqLNYcQ9AqAmCJHUmLnkYBcQnMMnqWI8ZUM5Zc5PS5h56sOBE9jAvX1MiwOG3Q1OijziOjDL4Oez29kXVtogEn8T/riQIDAQAB";
        String privateKey = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAKrly7adLIZ4Gco6funHdEo1fCB9+oG5PW+Dmp7u2CljRPxJnHdJLJ84a5hDZTlNEDHkdVGJF2CFrIXVTOos1hxD0CoCYIkdSYueRgFxCcwyepYjxlQzllzk9LmHnqw4ET2MC9fUyLA4bdDU6KPOI6MMvg57Pb2RdW2iASfxP+uJAgMBAAECgYBLaSFuUIbQQ3jl4tAT6r5m7Jj8YXrD+XChn4oALdbW1IYhaX4QqQp2q9fWlY66LUZDLdOTChJ1tia927SX9Pb5Qic+1X9qRWGsqxxL0Nt1Hxzx849XBTyUif+lJQmXIbVt3b8UceFwP4KitOHkSHRH7xHR3A6iQG9SExmAj9yysQJBAOqZjoF2BhR4SZCmwLoN8UJINxEHnylp62rQeH/E52eMqjKoAGcP90CzFG6QqAeb4WqTDXWS0nmUcphcjNtdmfUCQQC6fKWkSaZSUZJjzLXGwZbpfHJrfuK/ezjTd/pZB1kNfNVlDInP13Tk1K4dzXPD+dCikYp7X1ouKfReP49F9WrFAkAIKHTbLaaCebhdLl9PWQLFwksf7XrsbsGAAmXeU5OUd0DH6C2zZ8u6VT/lpGQjd5k8Iob6SrPFBJODltxTGIstAkEApQMCdrqMpy5ywEg1WGeYJ4XsoN2nuhNIlyYEqZ5s2BhronxTvUtCrfDbGJfhJbn5OMBbrwsSeuK4zmSjXB3qmQJAJTRH7wRYVk6DCtZNVFNy4l3hZ0S4FkzNe70y6PhXoLkoI5Caw+CP0ZvgJM70pAPmKG5Cf16u7j15UwOZ10+T4g==";
        String content = "name=你好";
        String salt = "H#ylfin$20Y20_@^!XSMp";
        String encrypt = encrypt(content, publicKey, salt);
        System.out.println("加密后：" + encrypt);
        String decrypt = decrypt(encrypt, privateKey, salt);
        System.out.println("解密后：" + decrypt);
    }

}
